<?php
  if (!defined('XOOPS_ROOT_PATH')) { exit(); }
  
  $poll = new XoopsPoll($_POST['poll_id']);
  $poll->setVar('question', $_POST['question']);
  $poll->setVar('description', $_POST['description']);
  
  $posts = array('autohour','automin','automonth','autoday','autoyear','addmore');
  foreach ( $posts as $pkey ) {
    $$pkey = intval($_POST[$pkey]);
  }
  $end_time = $autoyear.'-'.$automonth.'-'.$autoday.' '.$autohour.':'.$automin;
  $poll->setVar("end_time", userTimeToServerTime(strtotime($end_time), $xoopsUser->timezone()));
  $poll->setVar('display', $_POST['display']);
  $poll->setVar('weight', $_POST['weight']);
  $poll->setVar('multiple', $_POST['multiple']);
  if ( $_POST['notify'] == 1 && $end_time > time() ) {
    $poll->setVar('mail_status', POLL_NOTMAILED);
  } else {
    $poll->setVar('mail_status', POLL_MAILED);
  }
  $poll->setVar("voteadd", $addmore);
  
  if ( !$poll->store() ) {
    echo $poll->getHtmlErrors();
    exit();
  }
  $i = 0;
  foreach ( $_POST['option_id'] as $opid ) {
    $option = new XoopsPollOption($opid);
    $option_text[$i] = trim ($_POST['option_text'][$i]);
    if ( $option_text[$i] != "" ) {
      $option->setVar('option_text', $option_text[$i]);
      $option->setVar('option_color', $_POST['option_color'][$i]);
      $option->store();
    } else {
      if ( $option->delete() != false ) {
        XoopsPollLog::deleteByOptionId($option->getVar('option_id'));
      }
    }
    $i++;
  }
  $poll->updateCount();
  include_once XOOPS_ROOT_PATH.'/class/template.php';
  xoops_template_clear_module_cache($xoopsModule->getVar('mid'));
  redirect_header('index.php',1,_AM_DBUPDATED);
?>